<!DOCTYPE html>
<html lang="zh-CN">
	<head>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>Document</title>
	</head>

	<body>
		<div id="app">
			<!-- 自定义事件名可以不写参数，不会默认传event -->
			<cpn @category="cpnClick"></cpn>
		</div>
		<template id="cpn">
			<div>
				<button v-for="item in categories" @click="btnClick(item)">
					{{item.name}}
				</button>
			</div>
		</template>

		<script src="../lib/vue.js"></script>
		<script>
			let cpn = {
				template: "#cpn",
				props: {},
				data() {
					return {
						categories: [
							{ id: "aaa", name: "热门推荐" },
							{ id: "bbb", name: "手机数码" },
							{ id: "ccc", name: "家用家电" },
							{ id: "ddd", name: "厨房生活" }
						]
					};
				},
				methods: {
					btnClick(item) {
						// 子组件发射方法$emit(自定义事件名，参数)
						this.$emit("category", item);
					}
				}
			};
			let vm = new Vue({
				el: "#app",
				data: {
					message: "hello"
				},
				methods: {
					cpnClick(item) {
						console.log(item);
					}
				},
				components: {
					cpn
				}
			});
		</script>
	</body>
</html>
